import { Node } from "cc";
import { IVector2, NodeHelper, Rectangle, Vector2, _logic, _timer, ccclass } from "../../../main/script/Main";
import { EEntityType } from "../../../main/script/module/define/LogicDefine";
import { Item } from "../Item";
import { Entity } from "./Entity";
import { TipEntity } from "./TipEntity";

const v2T = new Vector2()
const v2T2 = new Vector2()

@ccclass("DragEntity")
export class DragEntity extends Entity {

    public entityType = EEntityType.Drag
    public isPut = true
    public isUpdateStateMackine = true
    public isDestroy = false
    public isMoving() { return false }

    private cADrag: Node = null!

    public get item() { return this.getCacheComponent(Item) }

    public rect = new Rectangle()

    private tipAnimEntity: TipEntity = null!

    public itemPos = new Vector2()
    public dragPos = new Vector2()
    public itemInitPos = new Vector2()

    public _isLerpAnim = false
    public _lerpPos = new Vector2()
    private _lastChildIndex = -1
    private lerpRatio = .2

    protected onStateEnterExit(arg?: string): void {
        this.tipAnimStop()
        this._isLerpAnim = false
        this._lastChildIndex = -1
        NodeHelper.setPositionXY(this.cADrag, this.dragPos, false)
        super.onStateEnterExit()
    }

    protected onStateEnterReset(): void {
        super.onStateEnterReset()
        NodeHelper.getRectangle(this.rect, this.node.parent, false)

        Vector2.add(this.itemPos, this.node.parent.position, this.cADrag.position)

        Vector2.add(_logic.ten.dragCurPos, this.itemPos, this.cADrag.position)

        this.dragPos.set(this.cADrag.position)

        this._isLerpAnim = true
        this.updateView()
    }

    public getConfigId() {
        return _logic.ten.dragConfigId
    }

    protected onStateUpdateRun(): void {
        super.onStateUpdateRun()
        if (this._isLerpAnim) {
            Vector2.sub(v2T, this._lerpPos, this.itemPos)
            Vector2.lerp(v2T2, this.cADrag.position, v2T, this.lerpRatio * _timer.dtSecond)
            NodeHelper.setPositionXY(this.cADrag, v2T2, false)

            if (Vector2.distanceSqr(this.dragPos, v2T2) < 1) {
                if (this._lastChildIndex != -1)
                    this.node.setSiblingIndex(this._lastChildIndex)
                this._lastChildIndex = -1
            }
        }
    }

    public resetItemPos(_lerp = true) {
        this._lerpPos.set(this.getCurPos())
        this.lerpRatio = 0.2
        if (!_lerp) {
            Vector2.sub(v2T, this._lerpPos, this.itemPos)
            NodeHelper.setPositionXY(this.cADrag, v2T, false)
        }
    }

    public updateView() {
        if (_logic.ten.dragConfigId != -1) {
            this.item.setConfigFrame(_logic.ten.dragConfigId)
            this.item.scaleAnim(.3, null!, true)
            this.resetItemPos(false)
        } else {
            this.item.scaleAnim(0, null!, false)
            this.resetItemPos(false)
        }
        this.updateCountLabel()
    }

    public setItemPos(_pos: IVector2) {
        this._lerpPos.set(_pos)
        this.lerpRatio = 0.6
        if (this._lastChildIndex == -1) {
            this._lastChildIndex = this.node.getSiblingIndex()
            this.node.setSiblingIndex(this.node.parent.children.length - 1)
        }
    }

    public getCurPos() {
        return _logic.ten.dragCurPos
    }


    private updateCountLabel() {
        this.Label.string = _logic.ten.dragCount + ''
    }

    public itemScale() {
        return this.cADrag.scale.x
    }


    public remove(_complete?: () => void, _pos?: IVector2) {
        let entity = this.entityMgr.createCubeAnim()
        entity.startAnim(
            _logic.ten.dragConfigId,
            _pos || this._lerpPos,
            false,
            _complete
        )
    }

    public tipAnim() {
        this.tipAnimEntity = this.scene.entityMgr.createTip(this)
    }

    public tipAnimStop() {
        if (this.tipAnimEntity) {
            this.scene.entityMgr.remove(this.tipAnimEntity)
            this.tipAnimEntity = null!
        }
    }
}